<template>
  <Dialog
    :title="title"
    :fullscreen="true"
    :model-value="visible"
    width="60%"
    :before-close="closeForm"
  >
    <Form
      :schema="schema"
      :model="empEntry"
      @register="formRegister"
      label-width="120px"
      :isCol="false"
      :rules="rules"
    />
    <template #footer>
      <el-space>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-space>
    </template>
  </Dialog>
</template>

<script>
import { Dialog } from '@/components/Dialog'
import { setEmpEntry, getPostTitle } from '@/api/emp/emp'
import { HrItemDic } from '@/config/publicDic'
import { Form } from '@/components/Form'
import { useUserStoreWithOut } from '@/store/modules/user'
import { useForm } from '@/hooks/web/useForm'
const { formRegister, formMethods } = useForm()
const userStore = useUserStoreWithOut()
export default {
  components: {
    Dialog,
    Form
  },
  props: {
    emp: {
      type: Object,
      default: () => {
        return {
          CompanyId: null,
          EmpId: null,
          EmpName: null
        }
      }
    },
    isRetainTitle: {
      type: Boolean,
      default: null
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      schema: [],
      title: '人员调职',
      empEntry: {
        CompanyId: null,
        DeptId: null,
        Post: null,
        Title: null
      },
      rules: {
        CompanyId: [
          {
            required: true,
            message: '请选择任职公司',
            trigger: 'blur'
          }
        ],
        DeptId: [
          {
            required: true,
            message: '请选择部门',
            trigger: 'blur'
          }
        ],
        Title: [
          {
            required: true,
            message: '请选择职务',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    comId() {
      return userStore.comId
    }
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.resetForm()
          this.initScheme()
        }
      },
      immediate: true
    }
  },
  methods: {
    formRegister,
    initScheme() {
      const list = [
        {
          field: 'CompanyId',
          label: '任职公司',
          component: 'CompanySelect',
          componentProps: {
            parentId: this.comId,
            placeholder: '请选择任职公司',
            on: {
              change: this.companyChange
            }
          }
        },
        {
          field: 'DeptId',
          label: '入职部门',
          component: 'DeptSelect',
          componentProps: {
            companyId: this.empEntry.CompanyId,
            isChioseUnit: false,
            placeholder: '请选择入职部门',
            on: {
              change: this.deptChange
            }
          }
        },
        {
          field: 'Post',
          label: '岗位',
          component: 'TreeDicItem',
          componentProps: {
            dicId: HrItemDic.post,
            placeholder: '请选择入职岗位',
            multiple: true
          }
        },
        {
          field: 'Title',
          label: '职务',
          component: 'DicItem',
          componentProps: {
            dicId: HrItemDic.title,
            placeholder: '请选择入职职务',
            multiple: true
          }
        }
      ]
      if (this.isRetainTitle == null) {
        list.push({
          field: 'IsRetainTitle',
          label: '保留原职务',
          component: 'Switch',
          componentProps: {
            activeText: '是',
            inactiveText: '否'
          }
        })
      }
      this.schema = list
    },
    async save() {
      const data = await formMethods.validate()
      if (data == null) {
        return
      }
      this.set(data)
    },
    async set(data) {
      await setEmpEntry(this.emp.EmpId, data)
      this.$message({
        message: '保存成功!',
        type: 'success'
      })
      this.$emit('update:visible', false)
      this.$emit('close', true)
    },
    closeForm() {
      this.$emit('update:visible', false)
      this.$emit('close', false)
    },
    companyChange(val) {
      this.empEntry.CompanyId = val
      this.empEntry.DeptId = null
      this.empEntry.Title = null
      this.empEntry.Post = null
      this.initScheme()
    },
    async deptChange(val) {
      this.empEntry.DeptId = val
      if (val == null) {
        this.empEntry.Title = null
        this.empEntry.Post = null
      } else {
        const res = await getPostTitle(this.emp.EmpId, val)
        if (res) {
          this.empEntry.Title = res.TitleCode
          this.empEntry.Post = res.PostCode
        } else {
          this.empEntry.Title = []
          this.empEntry.Post = []
        }
        this.initScheme()
      }
    },
    resetForm() {
      this.title = '员工(' + this.emp.EmpName + ')调职'
      this.empEntry = {
        CompanyId: this.comId,
        Post: null,
        Title: null,
        DeptId: null,
        IsRetainTitle: this.isRetainTitle
      }
      formMethods.setValues(this.empEntry)
    }
  }
}
</script>
